import Link from "../../../components/Link"; import { useRouter } from "next/router"; import { useEffect, useState } from "react"; import Header from "../../../components/Header"; import apiOdoo from "../../../helpers/apiOdoo"; import { createSlug, getIdFromSlug } from "../../../helpers/slug"; import currencyFormat from "../../../helpers/currencyFormat"; import { LazyLoadImage } from "react-lazy-load-image-component"; import "react-lazy-load-image-component/src/effects/blur.css"; import ProductSlider from "../../../components/product/ProductSlider"; import Layout from "../../../components/Layout"; import { createOrUpdateItemCart } from "../../../helpers/cart"; import toast from "react-hot-toast"; export async function getServerSideProps( context ) { const { slug } = context.query; let product = await apiOdoo('GET', '/api/v1/product/' + getIdFromSlug(slug)); if (product.length == 1) { product = product[0]; product.description = product.description.replaceAll('
', '||p||'); product.description = product.description.replaceAll('>', '||/p||'); product.description = product.description.replace(/(<([^>]+)>)/gi, ' '); product.description = product.description.replaceAll('||p||', '
'); product.description = product.description.replaceAll('||/p||', '
'); product.description = product.description.trim(); } return { props: { product } }; } export default function ProductDetail({ product }) { const router = useRouter(); const { slug } = router.query; const [selectedVariant, setSelectedVariant] = useState(""); const [quantity, setQuantity] = useState("1"); const [similarProducts, setSimilarProducts] = useState(null); const [activeVariant, setActiveVariant] = useState({ id: product.id, code: product.code, price: product.lowest_price, stock: product.stock_total, weight: product.weight, attributes: '', }); useEffect(() => { if (product.variants.length == 1) { setSelectedVariant(product.variants[0].id); } }, [product]); useEffect(() => { setSimilarProducts(null); const getSimilarProducts = async () => { const dataSimilarProducts = await apiOdoo('GET', `/api/v1/product/${getIdFromSlug(slug)}/similar?limit=20`); setSimilarProducts(dataSimilarProducts); } if (slug) getSimilarProducts(); }, [slug]); useEffect(() => { if (selectedVariant != '') { let newActiveVariant = product.variants.filter((variant) => { return variant.id == selectedVariant; }); if (newActiveVariant.length == 1) { newActiveVariant = newActiveVariant[0]; setActiveVariant({ id: newActiveVariant.id, code: newActiveVariant.code, price: newActiveVariant.price, stock: newActiveVariant.stock, weight: newActiveVariant.weight, attributes: newActiveVariant.attributes.join(', '), }); } } }, [selectedVariant, product]); let onchangeVariant = (e) => { setSelectedVariant(e.target.value); setQuantity("1"); } let addItemToCart = () => { if (product.variant_total > 1 && !selectedVariant) { toast.error('Pilih varian terlebih dahulu untuk menambahkan ke keranjang', { duration: 2000 }); return false; } if (quantity > 0) { toast.success('Berhasil menambahkan ke keranjang', { duration: 1500 }); createOrUpdateItemCart(activeVariant.id, parseInt(quantity)); } else { toast.error('Jumlah barang yang ditambahkan minimal 1 pcs', { duration: 2000 }); } return true; } return ( <>Harga mulai dari:
) : ''} {product.lowest_price.discount_percentage > 0 ? ({currencyFormat(activeVariant.price.price)}
{activeVariant.price.discount_percentage}%{currencyFormat(activeVariant.price.price_discount)}
) : (Dapatkan harga terbaik, hubungi kami.
)}{product.variant_total} Varian
SKU-{activeVariant.id}
{activeVariant.code}
{activeVariant.stock > 0 ? (activeVariant.stock > 5 ? 'Lebih dari 5' : 'Kurang dari 5') : '0'}
{activeVariant.weight > 0 ? activeVariant.weight : '1'} KG